package com.fishingwithme.infrastructure.mapper;

import com.fishingwithme.domain.user.SpotUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Select;
import com.fishingwithme.infrastructure.dtos.UserPageDto;
import io.lettuce.core.dynamic.annotation.Param;

import org.apache.ibatis.annotations.Update;

@Mapper
public interface SpotUserMapper extends BaseMapper<SpotUser> {
    SpotUser findByOpenId(String openId);

    SpotUser findByUserId(Long userId);

    void insertUser(SpotUser user);

    SpotUser getUser();

    void updateUserEmail(Long userId, String email);
    void updateUserNickname(Long userId, String nickname);
    void updateUserAvatar(Long userId, String avatar);
    void updateUserGender(Long userId, Integer gender);
    void disableUser(Long userId, Boolean status);

    /**
     * 根据用户名查找用户
     * @param username 用户名
     * @return 用户实体
     */
    @Select("SELECT * FROM spot_user WHERE username = #{username}")
    SpotUser findByUsername(String username);

    /**
     * 分页查询用户列表
     */
    @Select("SELECT user_id, username, avatar_url, gender, register_time, email, phone_no, admin, status, created_at, updated_at " +
            "FROM spot_user ORDER BY created_at DESC")
    IPage<UserPageDto> selectUserPage(Page<UserPageDto> page);

    /**
     * 根据用户名模糊搜索用户，限制返回10条结果
     */
    @Select("SELECT user_id, username, avatar_url, gender, register_time, email, phone_no, admin, status, created_at, updated_at " +
            "FROM spot_user WHERE username LIKE CONCAT('%', #{username}, '%') ORDER BY created_at DESC LIMIT 10")
    List<UserPageDto> searchUsersByName(@Param("username") String username);

    /**
     * 切换用户管理员权限
     */
    @Update("UPDATE spot_user SET admin = #{isAdmin}, updated_at = NOW() WHERE user_id = #{userId}")
    void updateAdminStatus(@Param("userId") Long userId, @Param("isAdmin") Boolean isAdmin);
}
